﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Telerik.Windows.Controls;
using DunSilverlight.OrganziationService;
using DunSilverlight.Comm;
using DunSilverlight.ViewModel;

namespace DunSilverlight.Views
{
    public partial class OrgForm : UserControl
    {
        OrganizationServiceClient oser = new OrganizationServiceClient();
        OrganizationModel obj = new OrganizationModel();
        public OrgForm()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(UserPanel_Loaded);
            this.DataContext = obj;
            oser.GetUserOrgCompleted += new EventHandler<GetUserOrgCompletedEventArgs>(oser_GetUserOrgCompleted);
            oser.SaveOrUpdateCompleted += new EventHandler<SaveOrUpdateCompletedEventArgs>(oser_SaveOrUpdateCompleted);
            

        }

       


        void UserPanel_Loaded(object sender, RoutedEventArgs e)
        {
            //生成该用户具有权限的组织机构树

            oser.GetUserOrgAsync(AppGlobal.CurrentUser.Id);

        }

        void oser_GetUserOrgCompleted(object sender, GetUserOrgCompletedEventArgs e)
        {
            var root = JsonSerializerHelper.JsonToEntity<DunLibrary.Organization.Organization>(e.Result);
            if (root != null)
            {
                List<DunLibrary.Organization.Organization> list = new List<DunLibrary.Organization.Organization>();
                list.Add(root);
                bindChildren(list, root);
                cboxOrg.ItemsSource = list;

            }
        }



        private void bindChildren(List<DunLibrary.Organization.Organization> list, DunLibrary.Organization.Organization root)
        {
            if (root.Childs != null)
            {
                foreach (var child in root.Childs)
                {
                    list.Add(child);
                    bindChildren(list, child);
                }
            }
        }

        private void Save_Click(object sender, RoutedEventArgs e)
        {
            //obj.Parent.Id = (cboxOrg.SelectedItem as DunLibrary.Organization.Organization).Id;
            if (obj.Validation())
            {
                var entity = obj.ToEntity();
                oser.SaveOrUpdateAsync(JsonSerializerHelper.EntityToJson(entity));
            }
            else
            {
                Tools.ShowMessage("输入格式不正确", "", false);
            }
           
        }

        void oser_SaveOrUpdateCompleted(object sender, SaveOrUpdateCompletedEventArgs e)
        {
            (this.Parent as RadWindow).Close();
        }

        private void Cancel_Click(object sender, RoutedEventArgs e)
        {
            (this.Parent as RadWindow).Close();
        }
    }
}
